public class Solution03 {
    // 全局变量:存储叶子节点的路径
    // private ArrayList<int> leaf;
    private ArrayList<Integer> leaf;

    public int sumNumbers(TreeNode root){
        leaf = new ArrayList<>();
        dfs(root, 0);
        int sum = 0;
        for(int tmp : leaf)
            sum += tmp;
        return sum;
    }

    public void dfs(TreeNode node, int path){
        if(node.left == null && node.right == null){
            // leaf.offer(path);
            leaf.add(path * 10 + node.val);
            return;
        }

        if(node.left != null){
            dfs(node.left, path * 10 + node.val);
        }
        if(node.right != null){
            dfs(node.right, path * 10 + node.val);
        }
    }
}
